ARG IMAGE

FROM $IMAGE


# 配置软件源
RUN sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirror.nju.edu.cn|g' \
    -i /etc/yum.repos.d/CentOS-*.repo \
 && yum install -y epel-release \
 && sed -e 's!^metalink=!#metalink=!g' \
    -e 's!^#baseurl=!baseurl=!g' \
    -e 's!//download\.fedoraproject\.org/pub!//mirror.nju.edu.cn!g' \
    -e 's!//download\.example/pub!//mirror.nju.edu.cn!g' \
    -e 's!http://mirrors!https://mirrors!g' \
    -i /etc/yum.repos.d/epel*.repo 


# 安装编译环境
RUN yum -y install gcc gcc-c++ make rpm-build libcurl-devel rpm-build rpm-devel rpmdevtools systemd-units


# 依赖包
RUN yum reinstall --downloadonly --downloaddir ~/libcurl-devel libcurl-devel libcurl curl


# 构建 libfastcommon
ARG LIBFASTCOMMON_VERSION
ENV LIBFASTCOMMON_VERSION=$LIBFASTCOMMON_VERSION

RUN rpmdev-setuptree \
 && curl -o ~/rpmbuild/SOURCES/libfastcommon-${LIBFASTCOMMON_VERSION}.tar.gz https://ghproxy.com/https://github.com/happyfish100/libfastcommon/archive/refs/tags/V${LIBFASTCOMMON_VERSION}.tar.gz \
 && tar xf ~/rpmbuild/SOURCES/libfastcommon-${LIBFASTCOMMON_VERSION}.tar.gz --strip-components=1 -C ~/rpmbuild/SPECS libfastcommon-${LIBFASTCOMMON_VERSION}/libfastcommon.spec \
 && rpmbuild -bb  ~/rpmbuild/SPECS/libfastcommon.spec \
 && rpm -ivh ~/rpmbuild/RPMS/x86_64/libfastcommon-*.rpm \
 && mv ~/rpmbuild/RPMS/x86_64 ~/libfastcommon-${LIBFASTCOMMON_VERSION} \
 && rm -rf ~/rpmbuild


# 构建 fastdfs
ARG FASTDFS_VERSION
ENV FASTDFS_VERSION=$FASTDFS_VERSION

RUN rpmdev-setuptree \
 && curl -o ~/rpmbuild/SOURCES/fastdfs-${FASTDFS_VERSION}.tar.gz https://ghproxy.com/https://github.com/happyfish100/fastdfs/archive/refs/tags/V${FASTDFS_VERSION}.tar.gz \
 && tar xf ~/rpmbuild/SOURCES/fastdfs-${FASTDFS_VERSION}.tar.gz --strip-components=1 -C ~/rpmbuild/SPECS fastdfs-${FASTDFS_VERSION}/fastdfs.spec \
 && sed -i "s#%define FDFSVersion .*#%define FDFSVersion ${FASTDFS_VERSION}#" ~/rpmbuild/SPECS/fastdfs.spec \
 && sed -i '78a install -m 0644 conf/http.conf %{buildroot}%{_sysconfdir}/fdfs/http.conf' ~/rpmbuild/SPECS/fastdfs.spec \
 && sed -i '79a install -m 0644 conf/mime.types %{buildroot}%{_sysconfdir}/fdfs/mime.types' ~/rpmbuild/SPECS/fastdfs.spec \
 && sed -i '125a /etc/fdfs/http.conf' ~/rpmbuild/SPECS/fastdfs.spec \
 && sed -i '126a /etc/fdfs/mime.types' ~/rpmbuild/SPECS/fastdfs.spec \
 && rpmbuild -bb  ~/rpmbuild/SPECS/fastdfs.spec \
 && rpm -ivh ~/rpmbuild/RPMS/x86_64/fastdfs-*.rpm \
 && rpm -ivh ~/rpmbuild/RPMS/x86_64/libfdfsclient-*.rpm \
 && mv ~/rpmbuild/RPMS/x86_64 ~/fastdfs-${FASTDFS_VERSION} \
 && rm -rf ~/rpmbuild


# 构建 fastdfs-nginx
ENV BASE_CONFIGURE_ARGS=' \
        --prefix=%{_sysconfdir}/nginx \
        --sbin-path=%{_sbindir}/nginx \
        --modules-path=%{_libdir}/nginx/modules \
        --conf-path=%{_sysconfdir}/nginx/nginx.conf \
        --error-log-path=%{_localstatedir}/log/nginx/error.log \
        --http-log-path=%{_localstatedir}/log/nginx/access.log \
        --pid-path=%{_localstatedir}/run/nginx.pid \
        --lock-path=%{_localstatedir}/run/nginx.lock \
        --http-client-body-temp-path=%{_localstatedir}/cache/nginx/client_temp \
        --http-proxy-temp-path=%{_localstatedir}/cache/nginx/proxy_temp \
        --http-fastcgi-temp-path=%{_localstatedir}/cache/nginx/fastcgi_temp \
        --http-uwsgi-temp-path=%{_localstatedir}/cache/nginx/uwsgi_temp \
        --http-scgi-temp-path=%{_localstatedir}/cache/nginx/scgi_temp \
        --with-pcre=../pcre-${PCRE_VERSION} \
        --with-zlib=../zlib-${ZLIB_VERSION} \
        --with-openssl=../openssl-${OPENSSL_VERSION} \
        --add-module=../fastdfs-nginx-module-${FASTDFS_NGX_VERSION}/src \
        --user=%{nginx_user} \
        --group=%{nginx_group} \
        --with-http_gzip_static_module \
        --with-http_gunzip_module \
        --with-http_ssl_module \
        --with-http_v2_module \
        --with-stream'


ADD .source/SPECS /SPECS
ADD .source/SOURCES /SOURCES

ARG NGINX_VERSION
ARG ZLIB_VERSION=1.2.13
ARG PCRE_VERSION
ARG OPENSSL_VERSION
ARG FASTDFS_NGX_VERSION
ARG PACKAGE_VENDOR=NGINX Packaging <nginx-packaging@f5.com>

ARG BASE_RELEASE=1

ENV ZLIB_VERSION=$ZLIB_VERSION
ENV PCRE_VERSION=$PCRE_VERSION
ENV OPENSSL_VERSION=$OPENSSL_VERSION
ENV FASTDFS_NGX_VERSION=$FASTDFS_NGX_VERSION
ENV NGINX_VERSION=$NGINX_VERSION

RUN rpmdev-setuptree \
 && curl -o ~/rpmbuild/SOURCES/zlib-${ZLIB_VERSION}.tar.gz http://www.zlib.net/zlib-1.2.13.tar.gz --progress-bar \
 && curl -o ~/rpmbuild/SOURCES/zlib-${ZLIB_VERSION}.tar.gz http://www.zlib.net/zlib-${ZLIB_VERSION}.tar.gz --progress-bar \
 && curl -o ~/rpmbuild/SOURCES/nginx-${NGINX_VERSION}.tar.gz https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz --progress-bar \
 && curl -o ~/rpmbuild/SOURCES/pcre-${PCRE_VERSION}.tar.gz https://nchc.dl.sourceforge.net/project/pcre/pcre/${PCRE_VERSION}/pcre-${PCRE_VERSION}.tar.gz --progress-bar \
 && curl -o ~/rpmbuild/SOURCES/openssl-${OPENSSL_VERSION}.tar.gz https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz --progress-bar \
 && curl -o ~/rpmbuild/SOURCES/fastdfs-nginx-module-${FASTDFS_NGX_VERSION}.tar.gz https://codeload.github.com/happyfish100/fastdfs-nginx-module/tar.gz/refs/tags/V${FASTDFS_NGX_VERSION} --progress-bar \
 && mv /SOURCES/* ~/rpmbuild/SOURCES \
 && cat /SPECS/nginx.spec.in | sed \
                -e "s#%%PACKAGE_VENDOR%%#${PACKAGE_VENDOR}#g" \
                -e "s#%%BASE_RELEASE%%#${BASE_RELEASE}#g" \
                -e "s#%%BASE_VERSION%%#${NGINX_VERSION}#g" \
                -e "s#%%BASE_CONFIGURE_ARGS%%#${BASE_CONFIGURE_ARGS}#g" \
                -e "s#%%ZLIB_VERSION%%#${ZLIB_VERSION}#g" \
                -e "s#%%PRCE_VERSION%%#${PCRE_VERSION}#g" \
                -e "s#%%OPENSSL_VERSION%%#${OPENSSL_VERSION}#g" \
                -e "s#%%FASTDFS_NGX_VERSION%%#${FASTDFS_NGX_VERSION}#g" \
                > ~/rpmbuild/SPECS/nginx.spec \
 && bash \
 && rpmbuild -bb  ~/rpmbuild/SPECS/nginx.spec \
 && rpm -ivh ~/rpmbuild/RPMS/x86_64/nginx-*.rpm \
 && mv ~/rpmbuild/RPMS/x86_64 ~/nginx-${NGINX_VERSION} \
 && rm -rf ~/rpmbuild


WORKDIR /root/rpmbuild/RPMS/x86_64
